﻿namespace com.stifel.BranchMgmt
{
    using System;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.DateTimeExtensions;

    using Microsoft.SharePoint;

    /// <summary>
    /// List Item Events
    /// </summary>
    public class StifelHardwareEventReceiver : SPItemEventReceiver
    {
        #region Fields

        StifelBranchConfiguration _configuration;

        #endregion Fields

        #region Methods

        /// <summary>
        /// The list received a context event.
        /// </summary>
        public override void ContextEvent( SPItemEventProperties properties )
        {
            base.ContextEvent( properties );
        }

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded( SPItemEventProperties properties )
        {
            base.ItemAdded( properties );
        }

        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding( SPItemEventProperties properties )
        {
            base.ItemAdding( properties );
        }

        /// <summary>
        /// An attachment was added to the item.
        /// </summary>
        public override void ItemAttachmentAdded( SPItemEventProperties properties )
        {
            base.ItemAttachmentAdded( properties );
        }

        /// <summary>
        /// An attachment is being added to the item.
        /// </summary>
        public override void ItemAttachmentAdding( SPItemEventProperties properties )
        {
            base.ItemAttachmentAdding( properties );
        }

        /// <summary>
        /// An attachment was removed from the item.
        /// </summary>
        public override void ItemAttachmentDeleted( SPItemEventProperties properties )
        {
            base.ItemAttachmentDeleted( properties );
        }

        /// <summary>
        /// An attachment is being removed from the item.
        /// </summary>
        public override void ItemAttachmentDeleting( SPItemEventProperties properties )
        {
            base.ItemAttachmentDeleting( properties );
        }

        /// <summary>
        /// An item was checked in.
        /// </summary>
        public override void ItemCheckedIn( SPItemEventProperties properties )
        {
            base.ItemCheckedIn( properties );
        }

        /// <summary>
        /// An item was checked out.
        /// </summary>
        public override void ItemCheckedOut( SPItemEventProperties properties )
        {
            base.ItemCheckedOut( properties );
        }

        /// <summary>
        /// An item is being checked in.
        /// </summary>
        public override void ItemCheckingIn( SPItemEventProperties properties )
        {
            base.ItemCheckingIn( properties );
        }

        /// <summary>
        /// An item is being checked out.
        /// </summary>
        public override void ItemCheckingOut( SPItemEventProperties properties )
        {
            base.ItemCheckingOut( properties );
        }

        /// <summary>
        /// An item was deleted.
        /// </summary>
        public override void ItemDeleted( SPItemEventProperties properties )
        {
            base.ItemDeleted( properties );
        }

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting( SPItemEventProperties properties )
        {
            base.ItemDeleting( properties );

            int id = properties.ListItemId;
            StifelHardwareDao dao = new StifelHardwareDao( properties.WebUrl );
            StifelHardware o = dao.Get( id );
            if ( o != null )
            {
                Notifications n = o.Notifications;
                removeNotifications( properties, n ); // remove notifications from list
            }
        }

        /// <summary>
        /// A file was converted.
        /// </summary>
        public override void ItemFileConverted( SPItemEventProperties properties )
        {
            base.ItemFileConverted( properties );
        }

        /// <summary>
        /// A file was moved.
        /// </summary>
        public override void ItemFileMoved( SPItemEventProperties properties )
        {
            base.ItemFileMoved( properties );
        }

        /// <summary>
        /// A file is being moved.
        /// </summary>
        public override void ItemFileMoving( SPItemEventProperties properties )
        {
            base.ItemFileMoving( properties );
        }

        /// <summary>
        /// An item was unchecked out.
        /// </summary>
        public override void ItemUncheckedOut( SPItemEventProperties properties )
        {
            base.ItemUncheckedOut( properties );
        }

        /// <summary>
        /// An item is being unchecked out.
        /// </summary>
        public override void ItemUncheckingOut( SPItemEventProperties properties )
        {
            base.ItemUncheckingOut( properties );
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated( SPItemEventProperties properties )
        {
            base.ItemUpdated( properties );

            DateTime eod = DateTime.Now.GetEndOfDay( );
            int id = properties.ListItemId;
            StifelHardwareDao dao = new StifelHardwareDao( properties.WebUrl );
            StifelHardware o = dao.Get( id );
            if ( o != null )
                if ( !o.HasScheduledMaintenance || // no scheduled maintenance window
                    o.HasScheduledMaintenance && !o.ScheduledMaintenance.HasValue || // schedule maintenance window, but no scheduled maintenance date
                    o.HasScheduledMaintenance && o.ScheduledMaintenance.Value.CompareTo( eod ) > 0 ) // scheduled maintenance window and schedule maintenance date greater than end of day
                {
                    if ( o.Notifications != null && o.Notifications.Items.Count > 0 )
                    {
                        Notifications n = o.Notifications;
                        o.HardwareNotification = string.Empty;
                        EventFiringEnabled = false;
                        dao.Update( o );
                        EventFiringEnabled = true;
                        removeNotifications( properties, n ); // remove notifications from list
                    }
                }
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating( SPItemEventProperties properties )
        {
            base.ItemUpdating( properties );
        }

        StifelBranchConfiguration getConfiguration( SPItemEventProperties properties )
        {
            if ( _configuration == null )
            {
                StifelBranchConfigurationDao dao = new StifelBranchConfigurationDao( properties.WebUrl );
                _configuration = dao.Get( );
            }
            return _configuration;
        }

        void removeNotifications( SPItemEventProperties properties, Notifications notifications )
        {
            if ( notifications != null && notifications.Items.Count > 0 )
            {
                StifelBranchConfiguration config = getConfiguration( properties );
                string url = config == null ? string.Empty : config.SiteURLForTheBranchHardwareMaintenanceNotificationUrl;
                if ( !string.IsNullOrEmpty( url ) )
                {
                    StifelNotificationDao dao = new StifelNotificationDao( url );
                    foreach ( Notification n in notifications.Items )
                    {
                        if ( n != null )
                            dao.Delete( n.UniqueId );
                    }
                }
            }
        }

        #endregion Methods
    }
}